package com.jd.mysql.mgr.controller.mgrdatabase;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.jd.mysql.mgr.dto.MgrDatabaseDTO;
import com.jd.mysql.mgr.pojo.MgrDatabase;
import com.jd.mysql.mgr.pojo.MgrMachine;
import com.jd.mysql.mgr.service.IMgrDatabaseService;
import com.jd.mysql.mgr.service.IMgrMachineService;
import com.jd.mysql.mgr.util.GlobalStaticVariables;
import com.jd.mysql.mgr.util.PageUtil;
import com.jd.mysql.mgr.util.ParamUtil;

public class AddDatabaseController extends SimpleFormController {
	private static Integer limit = GlobalStaticVariables.limit;
	private IMgrDatabaseService mgrDatabaseService;
	private IMgrMachineService mgrMachineService;

	public AddDatabaseController() {
		this.setCommandClass(MgrDatabase.class);
	}

	public void setMgrMachineService(IMgrMachineService mgrMachineService) {
		this.mgrMachineService = mgrMachineService;
	}

	public void setMgrDatabaseService(IMgrDatabaseService mgrDatabaseService) {
		this.mgrDatabaseService = mgrDatabaseService;
	}

	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		// TODO Auto-generated method stub
		Integer masterId = ParamUtil.getIntParam(request, "masterId", 0);
		Integer machineId = ParamUtil.getIntParam(request, "machineId", 0);
		Integer id = ParamUtil.getIntParam(request, "id", 0);
		MgrDatabase mgrDatabase = (MgrDatabase) command;
		// mgrDatabase.setMasterId(masterId);
		MgrDatabase oldDatabase = null;
		if(id!=0){
			oldDatabase = mgrDatabaseService.findMgrDatabaseById(id);
			if(oldDatabase!=null){
				if(oldDatabase!=null){
					oldDatabase.setApplyPerson(mgrDatabase.getApplyPerson());
					oldDatabase.setDatabaseVersion(mgrDatabase.getDatabaseVersion());
					oldDatabase.setDbCharacterSet(mgrDatabase.getDbCharacterSet());
					oldDatabase.setDbEngine(mgrDatabase.getDbEngine());
					oldDatabase.setDbName(mgrDatabase.getDbName());
					oldDatabase.setDbPort(mgrDatabase.getDbPort());
					oldDatabase.setIsMaster(mgrDatabase.getIsMaster());
					oldDatabase.setOperationSystem(mgrDatabase.getOperationSystem());
					oldDatabase.setProjectName(mgrDatabase.getProjectName());
					oldDatabase.setTableCount(mgrDatabase.getTableCount());
					oldDatabase.setUsePartition(mgrDatabase.getUsePartition());
					oldDatabase.setYn(true);
					mgrDatabaseService.updateMgrDatabase(oldDatabase);
				}
			}
		}else{
			try{
				mgrDatabase.setOnlineTime(new Date());
				mgrDatabase.setYn(true);
				MgrMachine mgrMachine = mgrMachineService.findMgrMachineById(machineId);
				if (mgrMachine == null) {
					return new ModelAndView("database/addDatabase", "msg",
							"This database should belong to a machine");
				} else {
					mgrDatabase.setMgrMachine(mgrMachine);
				}
				if (masterId != 0) {
					mgrDatabase.setMasterId(masterId);
				}
				mgrDatabaseService.saveMgrDatabase(mgrDatabase);
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		try {
			String index = request.getParameter("index");
			Map<String, Object> map = new HashMap<String, Object>();
			Integer start = PageUtil.setPageParam(map, index, limit);
			Integer groupId = (Integer)request.getSession().getAttribute("group");
			Long totalCount = mgrDatabaseService.getTotalCount(true, machineId,groupId);
			List<MgrDatabase> list = mgrDatabaseService.getMgrDatabaseList(
					start, limit, true, machineId,groupId);
			List<MgrDatabaseDTO> dtoList = new ArrayList<MgrDatabaseDTO>();
			if (list != null) {
				for (MgrDatabase mgrdatabase : list) {
					dtoList.add(mgrdatabase.toDTO());
				}
			}
			map.put("totalCount", totalCount);
			map.put("dtoList", dtoList);
			map.put("machineId", machineId);
			return new ModelAndView("database/databaseList", "map", map);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}
}
